package com.qf.service.impl;

import com.qf.mapper.AdminMapper;
import com.qf.pojo.Admin;
import com.qf.pojo.R;
import com.qf.pojo.UserDto;

import com.qf.service.AdminService;
import com.qf.util.RUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public R login(UserDto dto, HttpSession session) {
        //如果用户在一定的时间内，连续出错多次，应该锁定账号，该如何实现？
        //1.校验
        if (dto != null && dto.getAccount() != null) {
            //2.校验验证码
            if (dto.getCaptcha().equals(session.getAttribute("code").toString())) {
                //3.查询
                Admin admin = adminMapper.selectByName(dto.getAccount());
                //4.校验用户
                if (admin != null) {
                    //5.校验密码
                    if (admin.getPassword().equals(dto.getPass())) {
                        //6.登陆成功
                        session.setAttribute("admin", admin);
                        //7.记录操作日志
                        //logDao.insert(new AdminLog(admin.getId(), SystemConfig.ALOG_TYPE_LS, "登陆成功"));

                        return RUtil.ok(admin);
                    } else {
                        return RUtil.fail();
                        //logDao.insert(new AdminLog(admin.getId(), SystemConfig.ALOG_TYPE_LE, "密码不正确，登陆失败"));
                    }
                } else {
                    return RUtil.fail();
                }
            } else {
                R fail = RUtil.fail();
                fail.setMsg("yz");
                return fail;
            }
        } else {
            return RUtil.fail();
        }
    }

    @Override
    public R loginout(HttpSession session) {
        Admin admin = (Admin) session.getAttribute("admin");
        session.removeAttribute("admin");
        return RUtil.ok();
    }
}
